/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crm.benedicto.servlet;

import crm.benedicto.beans.Cliente;
import crm.benedicto.logica.ClienteLogica;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author User
 */
public class ServletCliente extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ServletCliente</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ServletCliente at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       

        int TipoOperacion = Integer.parseInt(request.getParameter("_Operacion"));
        ClienteLogica objController = new ClienteLogica();

        List lCliente;
        int IdCliente = 0;
        int validadni=0;
                
       
        if (!String.valueOf(request.getParameter("_CodigoCliente")).equals("")) {
            IdCliente = Integer.parseInt(request.getParameter("_CodigoCliente"));
        }
        
        
        
        Cliente ClienteBE = new Cliente();
        ClienteBE.setDni(request.getParameter("tbDni"));
        ClienteBE.setNombre(request.getParameter("tbNombres"));
        ClienteBE.setApellidopaterno(request.getParameter("tbApellidoPaterno"));
        ClienteBE.setApellidomaterno(request.getParameter("tbApellidoMaterno"));
        ClienteBE.setFechacontacto(request.getParameter("tbFechaContacto"));
        
               

        try {
            switch (TipoOperacion) {
                case 1:
                    lCliente = objController.ListarCliente(ClienteBE);
                    request.setAttribute("listaClientes", lCliente);
                    request.getRequestDispatcher("/ListarClientes.jsp").forward(request, response);
                    break;
                case 2:
                    ClienteBE.setCodigo(IdCliente);
                    objController.EliminarCliente(ClienteBE);
                    ClienteBE.setCodigo(0);
                    lCliente = objController.ListarCliente(ClienteBE);
                    request.setAttribute("listaClientes", lCliente);
                    request.getRequestDispatcher("/ListarClientes.jsp").forward(request, response);
                    break;
                case 3:
                    ClienteBE.setEmail(request.getParameter("tbEmail"));
                    ClienteBE.setTelefono(request.getParameter("tbTelefono"));
                    ClienteBE.setCelular(request.getParameter("tbCelular"));
                    
                    validadni= objController.InsertarCliente(ClienteBE);
                    if( validadni > 0) {
                        request.setAttribute("valida", validadni);
                        request.getRequestDispatcher("/MantenimientoCliente.jsp").forward(request, response);
                    } else{
                    
                    objController.InsertarCliente(ClienteBE);
                    request.getRequestDispatcher("/ListarClientes.jsp").forward(request, response);}
                    break;
                case 4:
                case 5:
                    ClienteBE.setCodigo(IdCliente);
                    if (TipoOperacion == 5) {
                        ClienteBE.setEmail(request.getParameter("tbEmail"));
                        ClienteBE.setTelefono(request.getParameter("tbTelefono"));
                        ClienteBE.setCelular(request.getParameter("tbCelular"));
                        ClienteBE.setFechacontacto(request.getParameter("tbFechaContacto"));
                        objController.ActualizarCliente(ClienteBE);
                    }
                    lCliente = objController.ListarCliente(ClienteBE);
                    request.setAttribute("objCliente", lCliente.get(0));
                    request.getRequestDispatcher("/MantenimientoCliente.jsp").forward(request, response);
                    break;
                case 6:
                    lCliente = objController.ListarCliente(ClienteBE);
                    request.setAttribute("listaClientes", lCliente);
                    request.getRequestDispatcher("/ListarClientes.jsp").forward(request, response);
                    break;
                    
            }
        } catch (Exception ex) {
            Logger.getLogger(ServletCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
